package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.*;
import com.gljx.web.service.CoachSubService;
import com.gljx.web.service.PersonEntrysService;
import com.gljx.web.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
@Transactional
@Service
public class CoachSubServiceImpl extends CommonDao implements CoachSubService {
    @Autowired
    CoachsubMapper coachsubMapper;
    @Autowired
    CoachsubentryMapper coachsubentryMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    PersonService personService;
    @Autowired
    PersonEntrysMapper personEntrysMapper;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.CoachsubMapper.pagecount", paramMap, null);
        return paramMap;
    }

    @Override
    public void addCoachsub(String state, Coachsub coachsub, String[] cfcoachid, String[] cfcoachno, String[] cfdqsub, String[] cfdqcartype, String[] cfxzsub,
                            String[] cfxzcartype, String[] cfdesc) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Coachsub coachsubtem = coachsubMapper.selectByPrimaryKey(coachsub.getFid());
            coachsub.setFcreatorid(coachsubtem.getFcreatorid());
            coachsub.setFcreatetime(coachsubtem.getFcreatetime());
            coachsubMapper.deleteByPrimaryKey(coachsub.getFid());
            coachsubentryMapper.deleteByFparentid(coachsub.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(coachsub.getFfivouchered() + "")) {
                coachsub.setFauditorid(coachsubtem.getFauditorid());
                coachsub.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(coachsub.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(coachsub.getFnumber()));
            }
        }

        //*获取uuid
        String uuid = GetFid.UUIDIntoFid();
        coachsub.setFid(uuid);
        if (cfcoachid != null || cfcoachid.length != 0) {
            //添加子类

            for (int i = 0; i < cfcoachid.length; i++) {
                Coachsubentry coachsubentry = new Coachsubentry();

                coachsubentry.setFid(GetFid.UUIDIntoFid());
                coachsubentry.setFparentid(uuid);
                coachsubentry.setCfjiaolianid(cfcoachid[i]);//教练id
                coachsubentry.setCfjiaolianno(cfcoachno[i]);//教练编码
                coachsubentry.setCfdqsub(Long.valueOf(cfdqsub[i] == null || cfdqsub[i] == "" ? "2" : cfdqsub[i]));
                coachsubentry.setCfdqcartype(cfdqcartype[i]);
                coachsubentry.setCfxzsub(Long.valueOf(cfxzsub[i] == null || cfxzsub[i] == "" ? "" : cfxzsub[i]));
                coachsubentry.setCfxzcartype(cfxzcartype[i]);
                coachsubentry.setCfdesc(cfdesc[i]);


                //如果传入状态为5则代表审核
                if ("5".equals(coachsub.getFfivouchered() + "")) {
                    //修改教练的科目 职员
                    Person person = personService.selTrainTypeById(cfcoachid[i]);
                    //查询子类
                    List<PersonEntrys> personEntrysList = personEntrysMapper.selectPersonEntrysByFparentid(person.getFid());
                    //如果有两条数据
                    PersonEntrys personEntrys = new PersonEntrys();
                    personEntrys.setFid(personEntrysList.get(0).getFid());
                    personEntrys.setCfkemu(coachsubentry.getCfxzsub());
                    personEntrys.setCfcartype(cfxzcartype[i]);

                    personEntrysMapper.updateByPrimaryKeySelective(personEntrys);
                }

                coachsubentryMapper.insertSelective(coachsubentry);
            }
            coachsubMapper.insertSelective(coachsub);
        }
    }

    @Override
    public Coachsub selectCoachsubByFid(String fid) {
        Coachsub coachsub = coachsubMapper.selectByPrimaryKey(fid);
        if (coachsub != null) {
            coachsub.setCoachsubentryList(coachsubentryMapper.selectCoachsubentryByFparentid(fid));
        }
        return coachsub;
    }

    @Override
    public void deleteByCoachsubKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();

        for (int i = 0; i < deleteId.length; i++) {
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            coachsubMapper.deleteByPrimaryKey(deleteId[i]);
            coachsubentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public ServerResponse examine(Coachsub coachsub, String state, String agreed, String auditcomment) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(coachsub.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {
                coachsub.setCfbillstate(Long.valueOf("5"));

                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                coachsub.setFlastupdatetime(createTime);
                //修改人id
                coachsub.setFauditorid(user.getFid());
                coachsubMapper.updateByPrimaryKeySelective(coachsub);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                coachsub.setCfbillstate(Long.valueOf("3"));
                coachsubMapper.updateByPrimaryKeySelective(coachsub);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            coachsub.setCfbillstate(Long.valueOf("3"));
            coachsub.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(coachsub.getFnumber(), auditcomment);
            coachsubMapper.updateByPrimaryKeySelective(coachsub);
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Coachsub> selectCoachSubAsXls(String jlName, String cfcoachno, String beginTime, String endTime, String fcompanyid) {
        return coachsubMapper.selectCoachSubAsXls(jlName, cfcoachno, beginTime, endTime, fcompanyid);
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.CoachsubMapper.pagemap", paramMap, null);
        return paramMap;
    }
}
